package com.fmblzf.library.timer;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.administrator.library.R;

/**
 * Created by Administrator on 2015/11/12.
 */
public class TimeAxis extends ImageView {

    private Context mContext;//上下文

    private Paint line;//画笔

    private int bitMapResId = -1;//处于活动中的图片样式
    private Bitmap focusBitmap = null;//时间轴的首项图片
    private int activeLineColor = -1;//活动中的线条颜色
    private int lineSize = 1;//线宽


    public TimeAxis(Context context) {
        this(context, null, 0);
    }

    public TimeAxis(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TimeAxis(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        this.mContext = context;

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TimeAxis);
        bitMapResId = ta.getResourceId(R.styleable.TimeAxis_activeBitMap, -1);
        activeLineColor = ta.getColor(R.styleable.TimeAxis_activeLineColor, -1);
        lineSize = (int)ta.getDimension(R.styleable.TimeAxis_lineSize, 1);

        initWidget();
    }

    /**
     * 初始化控件
     */
    private void initWidget() {
        //设置首项图片
        if (focusBitmap == null) {
            if (bitMapResId != -1) {
                focusBitmap = BitmapFactory.decodeResource(getResources(), bitMapResId);
            }
        }
        //创建画笔
        if (line == null) {
            line = new Paint();
            line.setColor(Color.parseColor("#ffaaaaaa"));
            line.setAntiAlias(true);
            line.setStrokeWidth(lineSize);
        }
    }

    /**
     * 设置处于活动状态
     */
    public void setActive() {
        line.setColor(activeLineColor);
        if (focusBitmap != null)
            setImageBitmap(focusBitmap);
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {

        canvas.drawLine(getMeasuredWidth() / 2, 0, getMeasuredWidth() / 2, getMeasuredHeight(), line);
        super.onDraw(canvas);
        canvas.restore();
    }
}
